//
//  CustomSegmentedView.m
//  caibb
//
//  Created by xh on 17/4/21.
//  Copyright © 2017年 xh. All rights reserved.
//

#define KSegmentedTextFontSize LSAutoVlaue(13, LSScreenWidth)
#import "UIView+Extension.h"
#import "CustomSegmentedView.h"


@interface CustomSegmentedView()

@property(copy,nonatomic)seletedSegmentedIndex seletedBlock;

@property(readwrite,nonatomic)NSMutableArray *btnArray;

@end

@implementation CustomSegmentedView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



/**
 *  CustomSegmentedView实例化
 *  @param frame             数据大小
 *  @param titlesArray       文本数组
 *  @param seletedTitleColor 选中问题的颜色
 *  @param normalTitleColor  正常状态下的颜色
 *  @param seletedBgColor    选中的背景
 *  @param normalBgColor     没有选中的背景
 *  @param bordeColor        边框的背景颜色
 *  @param borderWidth       边框大小
 *  @param seletedBlock      选中block回调
 *
 *  @return 实例化结果
 */
-(instancetype)initWidthCustomSegmentedViewFrame:(CGRect)frame titlesArray:(NSArray *)titlesArray seletedTitleColor:(UIColor *)seletedTitleColor normalTitleColor:(UIColor *)normalTitleColor  seletedBgColor:(UIColor *)seletedBgColor normalBgColor:(UIColor *)normalBgColor  bordeColor:(UIColor *)bordeColor borderWidth:(NSInteger)borderWidth seletedCustomViewIndex:(seletedSegmentedIndex)seletedBlock{
    
    self=[super initWithFrame:frame];
    
    if (self) {
        self.seletedBlock=seletedBlock;
        
        self.btnArray=[NSMutableArray arrayWithCapacity:titlesArray.count];
        
        [self addSubviewTitlesArray:titlesArray seletedTitleColor:seletedTitleColor normalTitleColor:normalTitleColor seletedBgColor:seletedBgColor normalBgColor:normalBgColor bordeColor:bordeColor borderWidth:borderWidth];
        
    }
    
    return self;
}


/**
 *  加入UI
 *
 *  @param titlesArray       文本数组
 *  @param seletedTitleColor 选中问题的颜色
 *  @param normalTitleColor  正常状态下的颜色
 *  @param seletedBgColor    选中的背景
 *  @param normalBgColor     没有选中的背景
*  @param  bordeColor        边框的颜色
 *  @param borderWidth       边框大小
 */
-(void)addSubviewTitlesArray:(NSArray *)titlesArray seletedTitleColor:(UIColor *)seletedTitleColor normalTitleColor:(UIColor *)normalTitleColor  seletedBgColor:(UIColor *)seletedBgColor normalBgColor:(UIColor *)normalBgColor  bordeColor:(UIColor *)bordeColor borderWidth:(NSInteger)borderWidth{
    
    
    self.layer.borderColor=bordeColor.CGColor;
    self.layer.borderWidth=borderWidth;
    self.layer.masksToBounds=YES;
    self.layer.cornerRadius=5;
    
    
    float btnWidth=(self.width-(titlesArray.count-1)*borderWidth)/titlesArray.count;
    
    float tempMax=0.0;
    
    for (int k=0; k<titlesArray.count; k++) {
        
        id parm=titlesArray[k];
        
        if ([parm isKindOfClass:[NSString class]]) {
            
            NSString *titles=(NSString *)parm;
            
            UIButton *customButton=[[UIButton alloc]initWithFrame:CGRectMake(tempMax, 0, btnWidth, self.height)];
            customButton.tag=k;
            
            customButton.titleLabel.font=[UIFont systemFontOfSize:KSegmentedTextFontSize];
            [customButton setTitle:titles forState:UIControlStateNormal];
            [customButton setTitle:titles forState:UIControlStateSelected];
            
            [customButton setTitleColor:normalTitleColor forState:UIControlStateNormal];
            [customButton setTitleColor:seletedTitleColor forState:UIControlStateSelected];
            [customButton setBackgroundImage:[self getImageWithBgColor:seletedBgColor] forState:UIControlStateSelected];
            [customButton setBackgroundImage:[self getImageWithBgColor:normalBgColor] forState:UIControlStateNormal];
            
            [customButton addTarget:self action:@selector(customSegmentedViewAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.btnArray addObject:customButton];
            [self addSubview:customButton];
            tempMax=customButton.maxX;
            
            if (k!=titlesArray.count-1) {
                
                UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(customButton.maxX, 0, borderWidth, customButton.height)];
                [lineView setBackgroundColor:bordeColor];
                [self addSubview:lineView];
                tempMax=lineView.maxX;
                
            }
            
            
        }
    }
   
   
    
}

/**
 *  默认开始选中第几个
 *
 *  @param index 选中Index
 */
-(void)selectedDefaultSegmentedIndex:(NSInteger )index{
    
    [self.btnArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *btn=(UIButton *)obj;
        if (idx==index) {
            btn.selected=YES;
        }else{
            btn.selected=NO;
        }
    }];
}


/**
 *  按钮选中事件
 *
 *  @param sender
 */
-(void)customSegmentedViewAction:(UIButton *)sender{
    
    [self selectedDefaultSegmentedIndex:sender.tag];
    
    if (self.seletedBlock) {
        self.seletedBlock(sender.tag);
    }
}


/**
 *  把颜色绘制成图片
 *
 *  @param color RGB
 *
 *  @return
 */
-(UIImage *)getImageWithBgColor:(UIColor *)color{
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}



@end
